package com.eoner.managerlibrary.cookie;

import android.content.Context;
import android.os.Build;
import android.webkit.CookieSyncManager;

import com.eoner.baselib.application.BaseApplication;
import com.eoner.baselibrary.utils.CookieNewUtil;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.system.SystemConfigStorage;

import java.util.ArrayList;
/**
 * 文件名：CookieManager
 * 描 述：cookie管理器
 * 作 者：gyq
 * 时 间：2021-1-22
 */
public class CookieManager {

    public static void setAppInfoCookies(Context context) {
        ArrayList<String> cookieList = new ArrayList<>();
        cookieList.add("platform=android");
        cookieList.add("device=" + CookieNewUtil.getDevicedId());
        cookieList.add("native_token=" + SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));
        cookieList.add("version=" + CookieNewUtil.getVersionName(BaseApplication.getContext()));
        cookieList.add("deviceToken=" + Config.DEVICE_TOKEN);
        cookieList.add("device_id=" + Config.DEVICE_ID+Config.GZG_TEST_USER);
        cookieList.add("mobile=" + SystemConfigStorage.getInstance().getMobile());
        cookieList.add("customer_type=" + SystemConfigStorage.getInstance().getCustomerType());
        syncCookie(context,".3dintime.com", cookieList);
    }

    /**
     * 同步cookie
     *
     * @param url 地址
     * @param cookieList 需要添加的Cookie值,以键值对的方式:key=value
     */
    private static void syncCookie (Context context, String url, ArrayList<String> cookieList) {
        CookieSyncManager.createInstance(context);
        android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (cookieList != null && cookieList.size() > 0) {
            for (String cookie : cookieList) {
                cookieManager.setCookie(url, cookie);
            }
        }
        cookieManager.setCookie(url, "Domain=.3dintime.com");
        String cookies = cookieManager.getCookie(url);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.flush();
        } else {
            CookieSyncManager.getInstance().sync();
        }
    }

}
